$(function () {
    $("#jqGrid").jqGrid({
        url: '/platform/oem/loanlist',
        datatype: "json",
        colModel: [
			{ label: 'id', name: 'loanId', index: 'loanId', width: 50 ,key:true,hidden:true},
			{ label: '名称', name: 'loanName', index: 'loanName'},
            { label: '还款方式', name: 'loanPayment', index: 'loanPayment'},
            { label: '费率', name: 'loanRate', index: 'loanRate'},
			// { label: 'app关键字', name: 'appKey', index: 'appKey', width: 80 },
			{ label: '状态', name: 'loanStatus', index: 'loanStatus',formatter: function(value, options, row){
				return value == 0?'<span class="label label-success">正常</span>':'<span class="label label-warning">下架</span>';
			}},
            { label: '创建时间', name: 'createDate', index: 'createDate'},
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,

		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "result.list",
            page: "result.currPage",
            total: "result.totalPage",
            records: "result.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条\
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
    vm.loadAllTypes();
    vm.loadSteps();
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		showInfo:false,
		title: null,
		isUpdate:false,
		loan: {steps:[],stepIds:[]},
        types:[],
        steps:[]
	},
	methods: {
		query: function () {
			vm.reload();
		},
        loadAllTypes:function () {
            $.getJSON("/platform/oem/typelist?page=1&limit=1000",null,function (r) {
                if (r.code == 200)
                    vm.types = r.result.list;
                else
                    alert(r.message);
            })
        },
		add: function(){
			vm.showList = false;
			vm.showInfo = true;
			vm.title = "新增";
			vm.loan = {steps:[],stepIds:[]};
            vm.isUpdate = false;
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
            // vm.app = $("#jqGrid").jqGrid().getRowData(appGradeId);
            vm.showList = false;
            vm.showInfo = true;
            vm.title = "修改";
            vm.getInfo(id);
            vm.isUpdate = true;
		},
        saveOrUpdate: function (event) {
		    vm.loan.steps = vm.loan.stepIds;
		    console.log("data",vm.loan);
			var urlStr = !vm.isUpdate ? "/platform/oem/loanadd" : "/platform/oem/loanupdate";
			$.ajax({
                url:urlStr,
                type:'POST',
                contentType:'application/json',
                data:JSON.stringify(vm.loan),
                success:function (r) {
                    if (r.code == 200)
                        alert("操作成功",function () {
                            vm.reload();
                        });
                    else
                        alert(r.message);
                }
            })
		},
        loadSteps:function () {
            $.getJSON("/platform/oem/steps",null,function (r) {
                if (r.code == 200)
                    vm.steps = r.result;
                else
                    alert(r.message);
            })
        },
		del: function (event) {
			var id = getSelectedRows();
			if(id == null){
				return ;
			}
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
				    url: "/platform/oem/loandel/"+id,
				    success: function(r){
						if(r.code == 200){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.message);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get("/platform/oem/loaninfo/"+id, function(r){
				if (r.code != 200){
				    alert(r.message);
                }
                vm.loan = r.result;
            });
		},
        reload: function (event) {
            vm.showList = true;
            vm.showInfo = false;
			$("#jqGrid").jqGrid('setGridParam',{
                page:1
            }).trigger("reloadGrid");
            vm.isUpdate = false;
		}
	}
});